//
//  OO_ClassListViewController.m
//  ShopProject
//
//  Created by macbookpro on 2018/9/27.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_ClassListViewController.h"
#import "XRWaterfallLayout.h"
#import "OO_ClassListCollectionViewCell.h"
@interface OO_ClassListViewController ()<UICollectionViewDataSource, XRWaterfallLayoutDelegate,UICollectionViewDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@end

@implementation OO_ClassListViewController
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    
    return _dataSource;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    XRWaterfallLayout *waterfall = [XRWaterfallLayout waterFallLayoutWithColumnCount:2];
    [waterfall setColumnSpacing:10 rowSpacing:10 sectionInset:UIEdgeInsetsMake(10, 10, 10, 10)];
    waterfall.delegate = self;
  [self.collectionView registerNib:[UINib nibWithNibName:@"OO_ClassListCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"OO_ClassListCollectionViewCell"];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView  setCollectionViewLayout:waterfall];
    WeakSelf
    [self.collectionView setRefreshWithHeaderBlock:^{
        [weakSelf loadData];
    } footerBlock:nil];
    [self.collectionView setupEmptyDataText:@"没有数据,点击重试" verticalOffset:0 emptyImage:[UIImage imageNamed:@"nodata"] tapBlock:^{
        [weakSelf.collectionView.mj_header beginRefreshing ];
    }];
    [self.collectionView.mj_header beginRefreshing];
    
}

#pragma mark - NetWorking
-(void)loadData{
    NSLog(@"%@",[NSString stringWithFormat:@"%@%@",HTTP,@"category#!method=post"]);
    [PPNetworkHelper POST:[NSString stringWithFormat:@"%@%@",HTTP,@"category#!method=post"] parameters:@{@"type":self.type} success:^(id responseObject) {
        NSDictionary *dict = (NSDictionary *)responseObject;
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:[OO_GoodsModel mj_objectArrayWithKeyValuesArray:dict[@"data"][@"data"][@"goods"]]];
        NSLog(@"%@",responseObject);
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
        
    }];
    
    
    
}


//根据item的宽度与indexPath计算每一个item的高度
- (CGFloat)waterfallLayout:(XRWaterfallLayout *)waterfallLayout itemHeightForWidth:(CGFloat)itemWidth atIndexPath:(NSIndexPath *)indexPath {

    return 250;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OO_ClassListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OO_ClassListCollectionViewCell" forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    OO_DetailViewController *detailVc = [OO_DetailViewController new];
    detailVc.model = self.dataSource[indexPath.row];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVc animated:1];
//    self.hidesBottomBarWhenPushed = NO;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
